﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Storage;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using eLuk.Entity;

// The data model defined by this file serves as a representative example of a strongly-typed
// model.  The property names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs. If using this model, you might improve app 
// responsiveness by initiating the data loading task in the code behind for App.xaml when the app 
// is first launched.

namespace eLuk.Data
{
	/// <summary>
	/// Creates a collection of groups and items with content read from a static json file.
	/// 
	/// SampleDataSource initializes with data read from a static json file included in the 
	/// project.  This provides sample data at both design-time and run-time.
	/// </summary>
	public sealed class eLukDataSource
	{
		private static eLukDataSource _sampleDataSource = new eLukDataSource();

		private eLukComponent _component = new eLukComponent();
		public eLukComponent eLukComponent
		{
			get { return this._component; }
		}

		public static async Task<eLukComponent> GetComponentAsync()
		{
			return await _sampleDataSource.GetSampleDataAsync();
			//return _sampleDataSource.Groups;
		}

		public static async Task<eLukSubComponent> GetGroupAsync( string uniqueId )
		{
			await _sampleDataSource.GetSampleDataAsync();
			// Simple linear search is acceptable for small data sets
			var matches = _sampleDataSource.eLukComponent.SubComponents.Where( ( group ) => group.UniqueId.Equals( uniqueId ) );
			if ( matches.Count() == 1 )
				return matches.First();
			return null;
		}

		public static async Task<eLukObject> GetItemAsync( string uniqueId )
		{
			await _sampleDataSource.GetSampleDataAsync();
			// Simple linear search is acceptable for small data sets
			var group = _sampleDataSource.eLukComponent.SubComponents.SingleOrDefault( g => g.UniqueId == uniqueId );
			if ( group != null )
				return group.eLukObject;
			return null;
		}

		private async Task<eLukComponent> GetSampleDataAsync()
		{
			//eLukGroup _group = new eLukGroup();

			Uri dataUri = new Uri( "ms-appx:///Assets" );
			string sLocation = dataUri.AbsolutePath + "/";
			if ( this._component == null )
				this._component = new eLukComponent();

			if ( this._component.SubComponents == null )
				this._component.SubComponents = new eLukSubComponents();

			this._component.SubComponents.Clear();

			this._component.SubComponents.Add( new eLukSubComponent( "email", "Email", "See what people are saying...", sLocation + "email.png", "Description", new EmailAccount() ) );
			this._component.SubComponents.Add( new eLukSubComponent( "calender", "Calender", "See what's on your schedule...", sLocation + "calender.png", "Description", new EmailAccount() ) );
			this._component.SubComponents.Add( new eLukSubComponent( "people", "People", "See who is in your contact...", sLocation + "contacts.png", "Description", new EmailAccount() ) );
			this._component.SubComponents.Add( new eLukSubComponent( "task", "Tasks", "See what's pending in your book...", sLocation + "tasks.png", "Description", new EmailAccount() ) );
			this._component.SubComponents.Add( new eLukSubComponent( "notes", "Notes", "See what you have written...", sLocation + "notes.png", "Description", new EmailAccount() ) );
			this._component.SubComponents.Add( new eLukSubComponent( "folders", "Folders", "See what you have saved in here...", sLocation + "folders.png", "Description", new EmailAccount() ) );

			if ( eLukSubComponent.GroupViews == null )
				eLukSubComponent.GroupViews = new List<KeyValuePair<string, Type>>();

			eLukSubComponent.GroupViews.Clear();

			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "email", typeof( eLukEmailView ) ) );
			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "calender", typeof( eLukEmailView ) ) );
			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "people", typeof( eLukEmailView ) ) );
			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "task", typeof( eLukEmailView ) ) );
			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "notes", typeof( eLukEmailView ) ) );
			eLukSubComponent.GroupViews.Add( new KeyValuePair<string, Type>( "folders", typeof( eLukEmailView ) ) );


			return this._component;

		}

	}

}